//
//  AccountSettingViewController.m
//  HikingManagerRemake
//
//  Created by FENGYL on 2016/12/14.
//  Copyright © 2016年 FENGYL. All rights reserved.
//

#import "AccountSettingViewController.h"
#import <BabyBluetooth.h>
#import <MBProgressHUD.h>

#define NUM @"0123456789"

@interface AccountSettingViewController ()<UITextFieldDelegate>

@property (strong , nonatomic) BabyBluetooth * baby;
@property (assign , nonatomic) BOOL isSave;

@property (strong , nonatomic) NSString * editUserName;
@property (strong , nonatomic) NSString * editHID;

@property (weak, nonatomic) IBOutlet UITextField *userNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *hidTextField;


@end

@implementation AccountSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.hidTextField.delegate = self;
    
    [self.hidTextField addTarget:self action:@selector(hidTextFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    [self.userNameTextField addTarget:self action:@selector(userNameTextFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    self.baby = [BabyBluetooth shareBabyBluetooth];
    self.isSave = NO;
    
}

-(void)viewWillAppear:(BOOL)animated{
    
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
    
    NSString * userName = [userDefaults stringForKey:USERDEFAULT_USER_NAME];
    
    NSString * userHid = [userDefaults stringForKey:USERDEFAULT_USER_HID];
    
    if (userName) {
        
        self.editUserName = userName;
        
    }
    
    if (userHid) {
        
        self.editHID = userHid;
        
    }
    
}

-(void)viewDidDisappear:(BOOL)animated{
    
    if (!self.isSave) {
        
        [self.baby cancelAllPeripheralsConnection];
        
    }
    
}

#pragma mark 预设名字和hid
-(void)setUserName:(NSString *)userName hid:(NSString *)hid{
    
    self.editUserName = userName;
    self.editHID = hid;
    
}

#pragma mark 限制输入长度
- (void)userNameTextFieldDidChange:(UITextField *)textField
{
    
    if (textField.text.length > 12) {
        textField.text = [textField.text substringToIndex:12];
    }
    
}

- (void)hidTextFieldDidChange:(UITextField *)textField
{
    
    if (textField.text.length > 4) {
        textField.text = [textField.text substringToIndex:4];
    }
    
}

#pragma mark 限制输入类型
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    //限制hid的输入类型
    if (textField == self.hidTextField) {
        
        NSCharacterSet * characterSet = [[NSCharacterSet characterSetWithCharactersInString:NUM] invertedSet];
        
        NSString * filtered = [[string componentsSeparatedByCharactersInSet:characterSet] componentsJoinedByString:@""];
        
        return [string isEqualToString:filtered];
        
        
    }
    
    return YES;
    
}

#pragma mark 随机hid
- (IBAction)getSystemRandomAssignment:(id)sender {
    
    NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"mmss"];
    formatter.timeZone = [NSTimeZone defaultTimeZone];
    NSString * dateTime = [formatter stringFromDate:[NSDate date]];
    
    [self.hidTextField setText:dateTime];
    
}


#pragma mark 点击保存
- (IBAction)saveAccountSetting:(id)sender {
    
    if ((self.hidTextField.text.length < 4 && self.hidTextField.text.length > 0) || (self.userNameTextField.text.length < 4 && self.userNameTextField.text.length > 0)) {
        
        MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        [hud.label setText:@"User name length cannot be less than 4\nHID must be 4 to 12 digits"];
        // 再设置模式
        hud.mode = MBProgressHUDModeText;
        
        hud.removeFromSuperViewOnHide = YES;
        
        [hud hideAnimated:YES afterDelay:0.5];
        
    }else if (self.hidTextField.text.length == 0 || self.userNameTextField.text.length == 0) {
        
        MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        [hud.label setText:@"User name or HID can not be empty!"];
        // 再设置模式
        hud.mode = MBProgressHUDModeText;
        
        hud.removeFromSuperViewOnHide = YES;
        
        [hud hideAnimated:YES afterDelay:0.5];
        
    }else{
        
//        if ([self.navigationItem.title isEqualToString:@"New Contact"]) {
//            
//            [self userSetUp];
//            
//        }else if([self.navigationItem.title isEqualToString:@"Add Account"]){
//            
//            [self friendsSetUp];
//            
//        }else if([self.navigationItem.title isEqualToString:@"Edit Contact"]){
//            
//            [self editContact];
//            
//        }else{
//            
//            [self editGroup];
//            
//        }
        
        if ([self.navigationItem.title isEqualToString:@"New Contact"]) {
            
            
            
        }else if ([self.navigationItem.title isEqualToString:@"Add Account"]){
            
            
            
        }else if ([self.navigationItem.title isEqualToString:@"Edit Contact"]){
            
            
            
        }else{
            
            
            
        }
        
        
    }
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.userNameTextField resignFirstResponder];
    [self.hidTextField resignFirstResponder];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
